Add and remove items

The Afosto Storefront Client makes it easy to manage items within your customer's cart. This guide will show you how to add and remove items using the client.

Adding Items to a Cart

To add one or more items to the cart, use the addCartItems function. The function takes an array of items to be added. Each item should be an object with sku and quantity properties.

Here's an example of how to add items to a cart:

1const addItemsToCart = async () => {
2  const cart = await client.addCartItems([
3    {
4      sku: 'sku-123',
5      quantity: 1,
6    },
7    {
8      sku: 'sku-456',
9      quantity: 2,
10    }
11  ]);
12};

In the example above, two items are added to the cart: one item with SKU 'sku-123' and one item with SKU 'sku-456'. The addCartItems function returns the updated cart.

Note: If autoCreateCart is set to true (the default setting), the client will automatically create a new cart if one doesn't already exist when you call addCartItems.

Removing Items from a Cart

To remove one or more items from the cart, use the removeCartItems function. This function takes an array of item IDs to be removed.

Here's an example of how to remove items from a cart:

1const removeItemsFromCart = async () => {
2  const cart = await client.removeCartItems(['item_id_1', 'item_id_2']);
3};

In the example above, the items with IDs item_id_1 and item_id_2 are removed from the cart. The removeCartItems function returns the updated cart.

By using these methods, you can effectively manage the items within your customer's cart using the Afosto Storefront Client.